package org.xdq.demo.components;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

@Component //说明受spring容器管理的对象
@Lazy(true)//设置懒加载（仅针对单例模式）
public class Computer3 {

    @Value("${my.info}")
    private String info;


    private Usb usb;

    public Computer3() {
        System.out.println("对象创建了！");
    }

    //定义初始化方法
    @PostConstruct //如果无法导入此注解，请在pom中导入javax.annotation-api
    public void f1(){
        System.out.println("对象初始化了！");
    }

    //定义销毁方法
    @PreDestroy //如果无法导入此注解，请在pom中导入javax.annotation-api
    public void f2(){
        System.out.println("对象销毁了！");
    }

    //@Autowired
    @Resource
    public void setUsb(@Qualifier("ausb") Usb usb) {
        this.usb = usb;
    }

    public void run(){
        System.out.println(info+"......");
        usb.work();
        System.out.println("....OK!");
    }

}
